import { Sprite } from '../engine/nodes/sprite.js';
import { vec3 } from '../engine/gl-matrix/index.js'
import { BoxCollider2D } from '../engine/core/boxCollider2D.js'
import { databus } from './databus.js'
import { Node } from '../engine/core/node.js';
import { AnimatorController } from '../engine/animation/animatorController.js';
import { AnimationClip } from '../engine/animation/animationClip.js';
import { AnimatorTransition } from '../engine/animation/animatorTransition.js';
import { music } from './music.js'

export class Enemy extends Node {
    constructor(x, y, z) {
        super();

        this.name = "enemy";
        this.speed = -databus.enemySpeed;//每毫秒移动距离
        this.translation = vec3.fromValues(x, y, z);
        this.scale = vec3.fromValues(0.12 * 0.9, 0.079 * 0.9, 1.0);

        this.boxCollider = new BoxCollider2D({
            isTrigger: true,
            min: vec3.fromValues(-1, -1, 0),
            max: vec3.fromValues(1, 1, 0)
        });

        this._createAnimation();
    }

    _createAnimation() {
        let animationClip = new AnimationClip({ loop: true, fps: 1 });
        let sprite = new Sprite({ url: "images/enemy.png" });
        sprite.material.renderOrder = 1;
        animationClip.addFrame(sprite);

        let animationClip2 = new AnimationClip({ loop: false, fps: 38 });

        for (let i = 1; i < 20; i++) {
            let explosionSprite = new Sprite({ url: "images/explosion" + i + ".png" });
            explosionSprite.material.renderOrder = 1;
            animationClip2.addFrame(explosionSprite);
        }

        animationClip2.addEventListener("play18", function () {
            this.stop();
        });

        let animatorController = new AnimatorController();
        animatorController.addClip(animationClip);
        animatorController.addClip(animationClip2);

        animatorController.addTransition(new AnimatorTransition({
            from: animationClip,
            to: animationClip2,
            paramName: "state",
            paramValue: "explosion",
            action: "equals"
        }));

        animatorController.addTransition(new AnimatorTransition({
            from: animationClip2,
            to: animationClip,
            paramName: "state",
            paramValue: "normal",
            action: "equals"
        }));

        this.animatorController = animatorController;

        this.add(animatorController);
    }

    onUpdate(timestamp, frameDelta) {
        if (databus.gameOver || this.visible == false) return;

        this.translation[1] += frameDelta * this.speed;

        if (this.translation[1] < -1.2) {
            this.visible = false;
            this.emit("recover");
        }
    }

    onTriggerEnter(node) {
        if (node.name != "enemy") {
            databus.score++;

            this.boxCollider.isTrigger = false;

            music.playExplosion();

            this.animatorController.setParams("state", "explosion");

            node.emit("applyDamage", this);
        }
    }

    reset(x, y, z) {
        vec3.set(this.translation, x, y, z);
        this.visible = true;
        this.boxCollider.isTrigger = true;
        this.animatorController.setParams("state", "normal");
    }
}